package com.example.permission.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.permission.common.CommonParam;
import com.example.permission.common.page.Page;
import com.example.permission.entity.dto.SysGroupDTO;
import com.example.permission.entity.dto.page.SysGroupPageDTO;
import com.example.permission.entity.po.SysGroup;
import com.example.permission.entity.vo.SysGroupTreeVO;
import com.example.permission.enums.DataStatusEnum;
import com.example.permission.mapper.SysGroupMapper;
import com.example.permission.service.SysGroupService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 组织机构(SysGroup)表服务实现类
 *
 * @author xuwq
 * @date 2020-05-09 15:56:07
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysGroupServiceImpl implements SysGroupService {

    @Autowired
    private SysGroupMapper sysGroupMapper;


    @Override
    public SysGroup insert(SysGroupDTO sysGroupDTO) {
        SysGroup sysGroup = new SysGroup();
        BeanUtils.copyProperties(sysGroupDTO, sysGroup);
        sysGroup.setCreateTime(new Date());
        sysGroup.setCreator(CommonParam.getInstance().getUserNo());
        this.sysGroupMapper.insert(sysGroup);
        return sysGroup;
    }

    @Override
    public SysGroup update(SysGroupDTO sysGroupDTO) {
        SysGroup sysGroup = new SysGroup();
        BeanUtils.copyProperties(sysGroupDTO, sysGroup);
        sysGroup.setUpdateTime(new Date());
        sysGroup.setUpdator(CommonParam.getInstance().getUserNo());
        this.sysGroupMapper.update(sysGroup);
        return this.queryById(sysGroup.getGroupId());
    }

    @Override
    public boolean deleteById(Integer groupId) {
        SysGroup sysGroup = new SysGroup();
        sysGroup.setGroupId(groupId);
        sysGroup.setDataStatus(DataStatusEnum.ABNORMAL.getCode());
        return this.sysGroupMapper.update(sysGroup) > 0;
    }

    @Override
    public SysGroup queryById(Integer groupId) {
        return this.sysGroupMapper.queryById(groupId);
    }

    @Override
    public Page<SysGroup> queryListByPage(SysGroupPageDTO sysGroupPageDTO) {
        int count = this.sysGroupMapper.queryCountByPage(sysGroupPageDTO);
        List<SysGroup> sysGroupList = this.sysGroupMapper.queryListByPage(sysGroupPageDTO);
        return new Page<>(count, sysGroupList);
    }

    /**
     * 递归树
     *
     * @param sysGroupTreeVOList
     * @param pId
     * @return
     */
    private static List<SysGroupTreeVO> makeTree(List<SysGroupTreeVO> sysGroupTreeVOList, Integer pId) {
        //子类
        List<SysGroupTreeVO> children = sysGroupTreeVOList.stream()
                .filter(x -> x.getPid().equals(pId)).collect(Collectors.toList());
        //后辈中的非子类
        List<SysGroupTreeVO> successor = sysGroupTreeVOList.stream()
                .filter(x -> !x.getPid().equals(pId)).collect(Collectors.toList());
        children.forEach(x -> makeTree(successor, x.getGroupId()).forEach(y -> x.getChildren().add(y)));
        return children;
    }

    @Override
    public List<SysGroup> queryGroupTree(Integer pid) {
        SysGroup sysGroup = new SysGroup();
        sysGroup.setPid(pid);
        sysGroup.setDataStatus(DataStatusEnum.NORMAL.getCode());
        return sysGroupMapper.queryAll(sysGroup);
    }

    @Override
    public List<SysGroupTreeVO> queryGroupTree() {
        SysGroup sysGroup = new SysGroup();
        sysGroup.setDataStatus(DataStatusEnum.NORMAL.getCode());
        List<SysGroup> sysGroupList = sysGroupMapper.queryAll(sysGroup);
        List<SysGroupTreeVO> sysGroupTreeVOList = JSON.parseArray(JSON.toJSONString(sysGroupList), SysGroupTreeVO.class);
        return makeTree(sysGroupTreeVOList, -1);
    }

}